package edu.bbu.cshu.wmpc.ioLocal;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.apache.log4j.Level;

import edu.bbu.cshu.wmpc.loggin.EventLogger;
/**
 * <ul>
 * <li><b><i>FileContentProvider</i></b></li>
 * <li style="list-style: none;"></li>
 * <li style="list-style: none;">
 * Enumeration that is used to extract the content of a file specified by an {@code ULR}, or from  an {@link InputStream} returned from a database. 
 * 
 * @author WMPC team
 * </li>
 * </ul>
 */ 
public enum FileContentProvider {
	/**
	 * <ul>
	 * <li><b><i>INSTANCE</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * Ensures that only a single instance of the enumeration exists.
	 * 
	 * </li>
	 * </ul>
	 */
	INSTANCE;
	/**
	 * <ul style="margin-top: 24px;">
	 * <li><b><i>contentFromURL</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * <p style="margin-left: 7px;">{@code public} {@link ArrayList}<{@link String}> {@code contentFromURL(}{@link String} {@code URL)}</p>
	 * <br>	
	 * Reads the contents of the file specified by the URL.  
	 * 
	 * @param URL - {@code String} containing the location of the file 
	 * @return 	  an {@code ArrayList<String>} containing the lines of the file
	 * </li>
	 * </ul>
	 */
	public ArrayList<String> contentFromURL(String URL) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("##### "+ URL.substring(URL.lastIndexOf('/')+1, URL.length()) +" #####");
		
		FileReader fr = null;
		BufferedReader br = null;
		
		try {
			fr = new FileReader(URL);
			br = new BufferedReader(fr);
			
			String tmp = null;
			
			while ((tmp = br.readLine()) != null) {
				list.add(tmp);
			}
		} catch(Exception ex) {
			String msg = "Error in edu.bbu.cshu.wmpc.ioLocal.FileContentProvider.contentFromURL: \n"+ex.getMessage();
            EventLogger.INSTANCE.writeToLog(Level.ERROR, msg);
		} finally {
			try {
				br.close();
				fr.close();
			} catch(Exception ex) {
				String msg = "Error in edu.bbu.cshu.wmpc.ioLocal.FileContentProvider.contentFromURL: \n"+ex.getMessage();
	            EventLogger.INSTANCE.writeToLog(Level.ERROR, msg);
			}
		}
		
		return list;
	}
	/**
	 * <ul style="margin-top: 24px;">
	 * <li><b><i>contentFromStream</i></b></li>
	 * <li style="list-style: none;"></li>
	 * <li style="list-style: none;">
	 * <p style="margin-left: 7px;">{@code public} {@link ArrayList}<{@link String}> {@code contentFromStream(}{@link InputStream} {@code stream)}</p>
	 * <br>	
	 * Reads the content of the given {@code InputStream}.  
	 * 
	 * @param stream - a files content within an {@code InputStream}
	 * @return 	  an {@code ArrayList<String>} with the contents of the given {@code InputStream}
	 * </li>
	 * </ul>
	 */
	@SuppressWarnings("finally")
	public ArrayList<String> contentFromStream(InputStream stream){
		ArrayList<String> list = new ArrayList<String>();
		
		int size;
		byte[] buffer = null;
		char[] chars = null;
		String contents;
		StringTokenizer st;
		
		try {
			size = stream.available();
			chars = new char[size];
			buffer = new byte[size];
			
			stream.read(buffer, 0, size);
			for (int i = 0; i < size; i++) {
				chars[i] = (char)(buffer[i]&0xff);
			}
			contents = new String(chars);
			
			st = new StringTokenizer(contents, "\n");
			while (st.hasMoreTokens()) {
				list.add(st.nextToken().replaceAll("\\r\\n|\\r|\\n", ""));
			}
			
		} catch (Exception ex) {
			String msg = "Error in edu.bbu.cshu.wmpc.ioLocal.FileContentProvider.contentFromStream: \n"+ex.getMessage();
            EventLogger.INSTANCE.writeToLog(Level.ERROR, msg);
		} finally {
			return list;
		}
	}
}
